Uec3.ShowSaleOrderWindow = Ext.extend(Ext.Window, {
	initPanel : function() {
		
		this.querySaleOrderListUrl = RES_PATH + '/saleOrder/listOnlineSaleOrders.do';
		
		this.createAllComb();
		
//		this.statusStore = this.createStatusStore();
		this.advancedQueryPanel = this.createAdvancedQueryPanel();
		this.openWindow = this.createOpenWindow();
		
		this.record = this.createRecord();
		this.colName = this.createColName();
		this.store = this.createStore();
		this.grid = this.createGrid();
		
		this.joinPanel = this.createJoinPanel();
		
		this.add(this.joinPanel);
		this.doLayout();
		this.show();
		this.creator.app.getDesktop().addAutoRun(this.getId());
		
		this.store.load();
		
//		this.captureStoreEvent();
		
	},
//	getOneWin : function() {
//		return this;
//	},
	// private
	moduleActionHtml : function(moduleId, action, value) {
		if(this.store.getById(value).data.workFlowId == '2') {
			action.desc = '处理订单';
		} else {
			action.desc = '查看单据';
		}
		return "<a style='font-size:12px' href=javascript:Uec3.App.getDesktop().getWindow('showSaleOrderWindow')." + action.func + "(" + value + "); >" + action.desc + "</a>";
//		alert(Uec3.ShowSaleOrderWindow.getOneWin().joinPanel);
//		return "<a style='font-size:12px' href=javascript:this.getOneWin()." + action.func + "(" + value + "); >" + action.desc + "</a>";

	},
	moduleActionRenderer : function(actions) {
		var ref = this;
		if (Ext.isArray(actions)) {
			return function(value, cellmeta, record, rowIndex, columnIndex, store) {
				var html = "";
				for (var i = 0; i < actions.length; i++) {
					var v = value;
					if (actions[i].reader) {
						v = actions[i].reader(value, cellmeta, record, rowIndex, columnIndex, store);
					}
					html += ref.moduleActionHtml(ref.creator.id, actions[i], v) + "&nbsp;&nbsp;";
				}
				return html;
			};
		} else {
			return function(value, cellmeta, record, rowIndex, columnIndex, store) {
				var v = value;
				if (actions.reader) {
					v = actions.reader(value, cellmeta, record, rowIndex, columnIndex, store);
				}
				return ref.moduleActionHtml(ref.creator.id, actions, v);
			};
		}
	},
	toOpenSaleOrder : function(id) {
		
		
		var rec = this.store.getById(id);
		var desktop = Uec3.App.getDesktop();
		if(rec.data.workFlowId + '' == '2') {

			var compId = rec.data.buyerId;
			
			desktop.launchWindowByParam("addAutoSaleOrderWindowCreator", {init_sellerCode : rec.data.codeOfSeller, id : rec.data.id});
			
			/*Ext.Ajax.request({
				url : RES_PATH + '/partner/checkPartner.do',
				params : {'companyId' : compId, 'relation' : '0'},
				success : function(data) {
					var bo = Ext.decode(data.responseText);
					if(bo && bo.result + '' == 'go_on') {
//						var desktop = Uec3.App.getDesktop();
						desktop.launchWindowByParam("addAutoSaleOrderWindowCreator", {init_sellerCode : rec.data.codeOfSeller, id : rec.data.id});
					} else {
						
						
						
						Ext.Msg.confirm("提示", '打开订单先要加对方为客户，确定添加吗？', function(btnId) {
							if(btnId == 'no') {
								return;
							} else {
								Ext.Ajax.request({
									url : RES_PATH + '/partner/addPartner.do',
									params : {'companyId' : compId, 'relation' : '0'},
									success : function(data) {
										var bo = Ext.decode(data.responseText);
										if(bo && bo.result + '' == 'ok') {
											Ext.Msg.confirm("提示", '客户添加成功,是否打开订单？', function(btnId) {
												if(btnId == 'no') {
													return;
												} else {
//													var desktop = Uec3.App.getDesktop();
													desktop.launchWindowByParam("addAutoSaleOrderWindowCreator", {init_sellerCode : rec.data.codeOfSeller, id : rec.data.id});
												}
											});
										} else {
											Ext.Msg.alert("提示", '客户添加失败..');
										}
									}
								});
							}
						});
					}
				}
			});*/
			
			
			/*desktop.launchWindowByParam("addAutoSaleOrderWindowCreator", {
//				init_status : rec.data.status,
				id : rec.data.id
			});*/
			
		} else if(rec.data.workFlowId + '' == '3') {
			desktop.launchWindowByParam("addOutSaleOrderWindowCreator", {
				init_sellerCode : rec.data.codeOfSeller,
				id : rec.data.id
			});
		}
	},
//	captureStoreEvent : function() {
//		Ext.util.Observable.capture(this.store, function(e) {
//			if(e == 'load') {
//				var records = this.store.getRange();
//				var ref = this;
//				Ext.each(records, function(record) {
//					var obj = Ext.get(SALE_OPEN_STRING + record.id + this.versionCode);
//					record.main_win = ref;
//					obj.on('click', function() {
//						this.main_win.toOpenSaleOrder(this);
//					}.createDelegate(record))
//				}.createDelegate(this));
//				
//			}
//		}.createDelegate(this));
//	},
	createJoinPanel : function() {
		return new Ext.FormPanel( {
		      items:[
		      	{
		          height:60,
		          layout:'hbox',
		          border:false,
		          layoutConfig: 
		             {     
		               padding:'5',
					   align:'middle' 
                  },
		          items : [
					 {				    
						xtype : 'label',
					    text : '浏览销售订单概要',
					    style : 'color:#000;font-size:26px;background:#fff'
					 }, {
		                xtype:'spacer',
                        flex:0.5
		             }, {
		             	xtype : 'button',
		             	text : '切换到明细浏览方式',
		             	margins:'0',
		             	handler : this.showDetailsByMode.createDelegate(this)
		             }, {
	                     xtype : 'numberfield',
	                     name : '_codeOfSeller'
	                 }, {
		                  xtype : 'button',
		                  text : '查找单据号',                   
		                  margins : '0 10',
		                  handler : this.searchByCode.createDelegate(this)
		             }, {
		                  xtype : 'button',
		                  text : '高级查找', 
		                  handler : this.queryByComplex.createDelegate(this)
		             }, {
		                  xtype:'spacer',
		                  flex:1
		             }, {
		                  xtype : 'button',
		                  text : '新增销售订单',
		                  margins:'0 50',
		                  handler : this.newSaleOrder.createDelegate(this)
		             }
		         ]
		      }, 
		      	this.grid
		    ]
		} );
	},
	
	queryByComplex : function() {
		this.advancedQueryPanel.getForm().reset();
		this.status_cmb.clearValue();
		this.openWindow.show();
	},
	searchByCode : function() {
		var value = this.joinPanel.getForm().findField('_codeOfSeller').getValue();
		var conditions = {codeOfSeller : value};
		this.store.baseParams = {conditions : Ext.util.JSON.encode(conditions)};
		this.store.load();
	},
	showDetailsByMode : function() {
		this.creator.app.getDesktop().launchWindowByParam("showSaleDetailsWindowCreator",{});
	},
	newSaleOrder : function() {
		this.creator.app.getDesktop().launchWindowByParam("addOutSaleOrderWindowCreator",{});
	},
	createAdvancedQueryPanel : function() {
		
		var ds_pur_status_select = getDictionaryStore(RES_PATH, {
			group : 'order_status'
		});
		
		this.status_cmb = getDictionaryComboBox(ds_pur_status_select, 'remote', 'status',
				'单据状态', 'status', 200);
			
		return new Ext.form.FormPanel({
		         labelSeparator : "：",
		         frame:true,
		         border:false,			    
		         items:[
		            {
		              xtype:'textfield',
				      width : 200,
		              fieldLabel:'单据号',
		              name:'codeOfSeller'
		            }, this.status_cmb ,{
		               xtype:'textfield',
				       width : 200,
		               fieldLabel:'供货商',
		               name:'partnerIdBySeller' 
		            },{
		               xtype:'datefield',
				       width : 200,
		               fieldLabel:'开始时间',
		               name:'gmtStart' ,
		               format:'Y-m-d'
		            },{
		               xtype:'datefield',
		               width : 200,
		               fieldLabel:'结束时间',
		               name:'gmtEnd',
		               format:'Y-m-d'
		            }
		         ],
		      buttons:[
		        { 
		          text:'查找',
				  handler : this.searchResultByCondition.createDelegate(this)
		        },{
		     	  text:'关闭',
		     	  handler : function(){	
		     	    this.openWindow.hide();
		     	  }.createDelegate(this)
		        }
		      ]
			
		});
	},
	searchResultByCondition : function() {
		var form_values = this.advancedQueryPanel.getForm().getValues();
	
		for (var j in form_values) {
			form_values[j] = castNoSelToNULL(form_values[j]);
		}
			
		form_values = Ext.util.JSON.encode(form_values);
		if(form_values) {
			this.store.baseParams = {'conditions' : form_values};
		} else {
			this.store.baseParams = {};
		}
		this.store.load();
		this.openWindow.hide();
	},
	createOpenWindow : function() {
		return new Ext.Window({
			title:'高级查找',
		    layout:'fit',
	    	width:380,
	    	closeAction:'hide',
	    	height:330,
			resizable :true,
			shadow : true,
			modal :true,
	    	closable:true,
	    	bodyStyle:'padding:5 5 5 5',
	    	animCollapse:true,
			items:[this.advancedQueryPanel]
	    });
	},
	createAllHidden : function() {
//		this.sale_id_hidden = ;
//		this.sale_buyer_id_hidden = ;
//		this.sale_areaId_hidden = ;
	},
	createAllComb : function() {
		// 来源下拉框
		var sourceType_store = getDictionaryStore(RES_PATH, {
			group : 'part_source'
		});
		this.source_cmb = getDictionaryComboBox(sourceType_store, 'local', 'part_source',
				'来源', 'source');
		sourceType_store.load();
		
		
//		// 支付方式下拉框
//		var payType_store = getDictionaryStore(RES_PATH, {
//			group : 'payType'
//		});
//		this.payType_cmb = getDictionaryComboBox(payType_store, 'local', 'paymentType',
//				'支付方式', null);
//		
//				
//		// 运输方式下拉框
//		var transportType_store = getDictionaryStore(RES_PATH, {
//			group : 'transportType'
//		});
//		this.transportType_cmb = getDictionaryComboBox(transportType_store, 'local',
//				'transportType', '运输方式', null);
	

	},
//	createStatusStore : function() {
//		return new Ext.data.JsonStore({
//           url : Uec3.App.Constants.homeUrl + "/dictionary/findByVarGroup.do?group=order_status",
//           root : "",
//           autoLoad : true,
//           fields : [{name:'varIndex',type:'int'},{name:'varName',type:'string'}]
//        }); 
//		
//	},
	createRecord : function() {
		return Ext.data.Record.create( [
			{
				name : 'id'
			}, {
				name : 'codeOfSeller'
			}, {
				name : 'partnerNameBySeller'
			}, {
				name : 'status'
			}, {
				name : 'statusDes'
			}, {
				name : 'amount'
			}, {
				name : 'currentor'
			}, {
				name : 'creator'
			}, {
				name : 'gmtCreate'
			}, {
				name : 'submitter'
			}, {
				name : 'gmtSubmit'
			}, {
				name : 'deliveryer'
			}, {
				name : 'gmtDelivery'
			}, {
				name : 'receiverName'
			}, {
				name : 'gmtAccept'
			}, {
				name : 'memoOfBuyer'
			}, {
				name : 'workFlowId'
			}, {
				name : 'buyerId'
			}
		] );
	},
	
	createColName : function() {
		
//		function createAddExecBtn(value, metadata, record, 
//			rowIndex, colIndex, store) {
//	
//			return '<div><a id="' + SALE_OPEN_STRING + record.id + this.versionCode + '" href="#">打开</a></div>';
//		}
		
		return new Ext.grid.ColumnModel( [ new Ext.grid.RowNumberer(),
			{
				header : '操    作',
				dataIndex : 'id',
				renderer : this.moduleActionRenderer({
					func : "toOpenSaleOrder"
//					desc : "打开:接收"
				})
			}, {
				header : '单 据 号',
				dataIndex : 'codeOfSeller'
			}, {
				header : '买      方',
				dataIndex : 'partnerNameBySeller'
			}, {
				header : '状      态',
				dataIndex : 'statusDes'
			}, {
				header : '金额(进价)',
				dataIndex : 'amount'
			}, {
				header : '当前处理人',
				dataIndex : 'currentor'
			}, {
				header : '创建人',
				dataIndex : 'creator'
			}, {
				header : '创建时间',
				dataIndex : 'gmtCreate'
			}, {
				header : '提交人',
				dataIndex : 'submitter'
			}, {
				header : '提交时间',
				dataIndex : 'gmtSubmit'
			}, {
				header : '发货人',
				dataIndex : 'deliveryer'
			}, {
				header : '发货时间',
				dataIndex : 'gmtDelivery'
			}, {
				header : '备    注',
				dataIndex : 'memoOfBuyer'
			} 
		]);
	},
	
	createStore : function() {
		return new Ext.data.JsonStore( {
			url : this.querySaleOrderListUrl,
			baseParams : {
//				orderId : this.orderId
			},
//			mode : "local",
			totalProperty : "totalProperty",
			root : "root",
			idProperty : "id",
			pruneModifiedRecords : true,
			fields : this.record
		});
	},

	createGrid : function() {
		return new Ext.grid.GridPanel( {
			title : '概要查询明细',
			cm : this.colName,
			store : this.store,
			iconCls : 'icon-grid',
			loadMask : {
				msg : '数据加载中...'
			},
			viewConfig : {
				forceFit:true
			},
//			width : '100%',
			height : 380,
	//		autoHeight : true,
			bbar: new Ext.PagingToolbar( {
	            pageSize : purOrSaleOrderPageSize,
	        	store : this.store,
	       	 	displayInfo : true,
	        	displayMsg : '第 {0} - {1} 条  共 {2} 条',
	        	emptyMsg : "没有记录"
	        	
	        } )
		} );
	}
	
});

Uec3.ShowSaleOrderButton = Ext.extend(Ext.Button, {
	
});

Uec3.ShowSaleOrderWindowCreator = Ext.extend(Ext.app.Module, {
			id : 'showSaleOrderWindowCreator',
			type : 'common/notepad',

			createWindow : function() {
				var desktop = this.app.getDesktop();
				var win = desktop.getWindow('showSaleOrderWindow');
				if (win) {
					win.toFront();
				} else {				
					win = desktop.createWindow({
								id : 'showSaleOrderWindow',
								title : '浏览销售订单概要',
								width : 1010,
								height : 500,
								iconCls : 'acc-icon',
								animCollapse : true,
								constrainHeader : true,
								maximizable : true,
								taskbuttonTooltip : "浏览销售订单概要",
								layout : "fit",
								items : []
							}, Uec3.ShowSaleOrderWindow);
							
					win.creator = this;
					win.versionCode = ++(PurchaseCount.count);
					win.initPanel();
				}
			}
		});